﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Soft.Net
{
    /// <summary>
    /// IP类型转换
    /// </summary>
    public sealed class IPTypeConverter : BaseStringToObjectTypeConverter
    {

        /// <summary>
        /// 从什么类型转换
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value">要转换的数据</param>
        /// <returns></returns>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            return IP.Parse((string)value);
        }

        /// <summary>
        /// 转换为 哪种类型
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value">要转换的数据</param>
        /// <param name="destinationType">要转换的数据类型</param>
        /// <returns></returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string) && value is TcpConnectIpAddress)
            {
                return value.ToString();
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }


    }
}
